<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Admin_Base extends Controller_Template {

	public $template = "_layout/_admin_tpl";

	public function renderView($view){
		$this->template->set('content', $view); // renders a view as a response
	}
	public $auth_required = FALSE;
 
  // Controls access for separate actions
  // 'adminpanel' => 'admin' will only allow users with the role admin to access action_adminpanel
  // 'moderatorpanel' => array('login', 'moderator') will only allow users with the roles login and moderator to access action_moderatorpanel
  public $secure_actions = FALSE;


  /**
   * The before() method is called before your controller action.
   * In our template controller we override this method so that we can
   * set up default values. These variables are then available to our
   * controllers if they need to be modified.
   */
  public function before()
  {
    parent::before();

    #Open session
    $this->session= Session::instance();

    #Check user auth and role
    $action_name = Request::current()->action();
    if (($this->auth_required !== FALSE && Auth::instance()->logged_in($this->auth_required) === FALSE)
            || (is_array($this->secure_actions) && array_key_exists($action_name, $this->secure_actions) && 
            Auth::instance()->logged_in($this->secure_actions[$action_name]) === FALSE))
				{
					if (Auth::instance()->logged_in()){
						HTTP::redirect('admin/user/index');
					}else{
						HTTP::redirect('admin/user/login');
					}
				}
 
  	    if ($this->auto_render)
  	    {
  	    	// Initialize empty values
  	    	$this->template->title   = '';
  	    	$this->template->content = '';
 
  			$this->template->styles = array();
  			$this->template->scripts = array();   
 
          }
      }
 
      /**
       * The after() method is called after your controller action.
       * In our template controller we override this method so that we can
       * make any last minute modifications to the template before anything
       * is rendered.
       */
      public function after()
      {
				if ($this->auto_render)
				{
					$styles = array(
						'css/main.css' => 'screen',
					);
		 
					$scripts = array(
						'js/jquery-1.3.2.min.js',
					);
		 
					$this->template->styles 	= array_merge( $this->template->styles, $styles );
					$this->template->scripts 	= array_merge( $this->template->scripts, $scripts );
		 
				}
				parent::after();
      }
	// function __get($template){
	// 	if(isset($template)){
	// 		return $this->$template;	
	// 	}
	// }

	// function __set($property, $value){
	// 	$this->$template = $value;
	// }
}